<?php

class Vega_Loader
{

    /**
     * Application directory name list
     *
     * @var array
     */
    protected $_dirs = array(
        
    );

    /**
     * Absolute paths of the directories
     */
    protected $_absoluteDirPaths = array(

    );

    protected $_rootPath = null;

    protected $_appPath = null;


    public function __construct($rootPath = null, $appPath = null)
    {
        $this->_rootPath = $rootPath;
        $this->_appPath  = $appPath;
    }

    /**
     * Set the root path
     *
     * @param string $path
     * @return Vega_Loader
     */
    public function setRootPath($path)
    {
        $this->_rootPath = $path;
        return $this;
    }

    /**
     * Returns the root path
     *
     * @return string
     */
    public function getRootPath()
    {
        return $this->_rootPath;
    }

    /**
     * Sets application path
     *
     * @param string $path
     * @return Vega_Loader
     */
    public function setAppPath($path)
    {
        $this->_appPath = $path;
        return $this;
    }

    /**
     * Returns application path
     *
     * @return string
     */
    public function getAppPath()
    {
        return $this->_appPath;
    }

    /**
     * Sets/adds an application directory
     *
     * @param string $keyName
     * @param string $dirName
     * @return Vega_Loader
     */
    public function setDir($keyName, $dirName, $isAppPath = true)
    {
        $this->_dirs[$keyName] = $dirName;

        if ($isAppPath)
        {
            $dirName = $this->_appPath . DIRECTORY_SEPARATOR . $dirName;
        }
        else
        {
            $dirName = $this->_rootPath . DIRECTORY_SEPARATOR . $dirName;
        }
        $this->_absoluteDirPaths[$keyName] = $dirName;
        return $this;
    }

    /**
     * Unsets an application directory
     *
     * @param string $keyName
     * @return Vega_Loader
     */
    public function unsetDir($keyName)
    {
        unset($this->_dirs[$keyName]);
        return $this;
    }

    /**
     * Returns an application directory name
     *
     * @param string $keyName
     * @return string
     */
    public function getDir($keyName)
    {
        return $this->_dirs[$keyName];
    }


    /**
     * Load a file from application directory
     *
     * @param string $type
     * @param string $path
     * @return mixed
     */
    public function load($type, $path, $returnVar = null, $ignoreErr = FALSE)
    {
        $realPath = $this->getPath($type, $path);

        if (!$realPath)
        {
            require_once 'Vega/Loader/Exception.php';
            throw new Vega_Loader_Exception('Cannot load file ' . $realPath . '. File does not exist.');
        }

        if (!file_exists($realPath))
        {
        	if($ignoreErr) return null;
        	
            require_once 'Vega/Loader/Exception.php';
            throw new Vega_Loader_Exception('Cannot load file at ' . $realPath . '. File not found.');
        }

        require_once $realPath;

        if ((null != $returnVar) && is_string($returnVar))
        {
            return ${$returnVar};
        }
        return $this;
    }

    /**
     * Loads a file with absolute path
     *
     * @param string $path
     * @param bool $once
     * @return Vega_Loader
     */
    public function loadAbsolutePath($path, $once = true)
    {
        if ($once)
        {
            require_once $path;
        }
        else
        {
            require $path;
        }
        return $this;
    }

    /**
     * Get the absolute path of the file
     *
     * @param string $type
     * @param string $path
     * @return string
     */
    public function getPath($type, $path = '')
    {
        if (!isset($this->_dirs[$type]))
        {
            return false;
        }
        return $this->_absoluteDirPaths[$type] . ($path ? DIRECTORY_SEPARATOR . $path : $path);
    }

    public function isAbsolutePath($path)
    {
        if ($path[0] == '/' || $path[0] == '\\' || 
            (strlen($path) > 3 && ctype_alpha($path[0]) && $path[1] == ':' && ($path[2] == '\\' || $path[2] == '/'))
        )
        {
            return true;
        }

        return false;
    }
}